(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("groupService", groupService);
	groupService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'gridService','modalService','$filter' ];
	function groupService($q, loggerFactory, commonService, ajaxServiceFactory, gridService, modalService, $filter) {
		var log = loggerFactory.getLogger("groupService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getParentCompanyCodeOptions : {
				url : 'do/cda/dataList/getParentCompanyCodeOptions',
				method : 'post'
			}
		});
		function getParentCompanyCodeOptions(ctrl) {
			var defer = $q.defer();
			var company = {};
			company.companyCode = ctrl.group.companyCode;
			ajaxService.getParentCompanyCodeOptions(company).then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}

		function initPrincipalRoles($scope, ctrl) {
			//init role grid
			ctrl.principalRoleGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.principalRole.tableConfigs.table, rowSelectionPrincipalRoleChanged);
			ctrl.principalRoleGridOptions.data = 'vm.group.principalRoles';
			ctrl.principalRoleGridOptions.verifyData = 'vm.groupOld.principalRoles';
			function rowSelectionPrincipalRoleChanged(eventRow, selectedRows) {
				ctrl.selectedPrincipalRoles = selectedRows;
			}
			
			ctrl.addRole = function(row) {
				return $q(function(resolved, rejected){
					var params = {"multiSelect": "true"};
					params.roleCodes = [];
					angular.forEach(ctrl.group.principalRoles, function(principalRole, index) {
						params.roleCodes.push(principalRole.roleCode);
					});
					modalService.openCustomDialogs({
						'dialogsModule' : 'cda.role',
						'params' : params,
						'btnCallback' : {
							'ok' : function(selectedRows) {
								if (!selectedRows || selectedRows.length == 0) {
									modalService.openErrorDialogs({
										messages : [ 'title.dialogs.info.norecsel' ]
									});
									return false;
								}
								if (!ctrl.group.principalRoles) {
									ctrl.group.principalRoles = [];
								}
								angular.forEach(selectedRows, function(selectedObj, index) {
									selectedRows[index].dataStatus = 'C';
									ctrl.group.principalRoles.push(selectedRows[index]);
								});
								
								event.preventDefault();
								event.stopPropagation();
							},
							'close' : function(){
								rejected();
							}
						}
					});
				});
				
			};
			
			ctrl.viewRole = function() {
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.right',
					'btnCallback' : {
						'ok' : function(selectedRows) {
							
						}
					}
				});
			};
			
			
		}
		function initParentGroups($scope, ctrl) {
			//init parent group grid
			ctrl.parentGroupGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.parentGroup.tableConfigs.table, rowSelectionParentGroupChanged);
			ctrl.parentGroupGridOptions.data = 'vm.group.parentGroups';
			ctrl.parentGroupGridOptions.verifyData = 'vm.groupOld.parentGroups';
			function rowSelectionParentGroupChanged(eventRow, selectedRows) {
				ctrl.selectedParentGroups = selectedRows;
			}
			ctrl.addParentGroup = function(row) {
				return $q(function(resolved, rejected){
					var params = {};
					params.multiSelect = true;
					params.groups = [];
					if (ctrl.group.groupCode) params.groups.push(ctrl.group.groupCode);
					angular.forEach(ctrl.group.parentGroups, function(parentGroup, index) {
						params.groups.push($filter('encode')(parentGroup.groupCode));
					});
					if (ctrl.group.groupCode){
						params.groupCode = $filter('encode')(ctrl.group.groupCode);
					}
					modalService.openCustomDialogs({
						'dialogsModule' : 'cda.group',
						'params' : params,
						'btnCallback' : {
							'ok' : function(selectedRows) {
								if (!selectedRows || selectedRows.length == 0) {
									modalService.openErrorDialogs({
										messages : [ 'title.dialogs.info.norecsel' ]
									});
									return false;
								}
								if (!ctrl.group.parentGroups) {
									ctrl.group.parentGroups = [];
								}
								angular.forEach(selectedRows, function(selectedObj, index) {
									selectedRows[index].dataStatus = 'C';
									ctrl.group.parentGroups.push(selectedRows[index]);
								});
								event.preventDefault();
								event.stopPropagation();
							},
							'close' : function(){
								rejected();
							}
						}
					});
				});
				
			};
			
		}
		function initSubGroups($scope, ctrl) {
			//init parent sub group grid
			//add sub data validator
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.subGroup.tableConfigs.table.validatorConfig = ctrl.validatorConfig.subGroups;
			ctrl.subGroupGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.subGroup.tableConfigs.table, rowSelectionSubGroupChanged);
			ctrl.subGroupGridOptions.data = 'vm.group.subGroups';
			ctrl.subGroupGridOptions.verifyData = 'vm.groupOld.subGroups';
			function rowSelectionSubGroupChanged(eventRow, selectedRows) {
				ctrl.selectedSubGroups = selectedRows;
			}
			ctrl.subGroupChanged = function (obj, form) {
				if(form.$dirty && 'C' != obj.dataStatus) obj.dataStatus = 'U';
			}
		}
		function initGroupUsers($scope, ctrl) {
			//init group user grid
			ctrl.userGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.user.tableConfigs.table, rowSelectionUserChanged);
			ctrl.userGridOptions.data = 'vm.group.groupUsers';
			ctrl.userGridOptions.verifyData = 'vm.groupOld.groupUsers';
			function rowSelectionUserChanged(eventRow, selectedRows) {
				ctrl.selectedUsers = selectedRows;
			}
			
			ctrl.addUser = function(row) {
			 return $q(function(resolved, rejected){
				var params = {};
				params.multiSelect = true;
				params.userCodes = [];
				angular.forEach(ctrl.group.groupUsers, function(groupUser, index) {
					params.userCodes.push($filter('encode')(groupUser.userCode));
				});
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.user',
					'params' : params,
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								return false;
							}
							if (!ctrl.group.groupUsers) {
								ctrl.group.groupUsers = [];
							}
							angular.forEach(selectedRows, function(selectedObj, index) {
								selectedRows[index].dataStatus = 'C';
								ctrl.group.groupUsers.push(selectedRows[index]);
							});
							event.preventDefault();
							event.stopPropagation();
						},
						'close' : function(){
							rejected();
						}
					}
				});
			 });
			};
			
		}
		function initBus($scope, ctrl) {
			
			//init bus grid
			ctrl.buGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.bu.tableConfigs.table, rowSelectionBuChanged);
			ctrl.buGridOptions.data = 'vm.group.bus';
			ctrl.buGridOptions.verifyData = 'vm.groupOld.bus';
			function rowSelectionBuChanged(eventRow, selectedRows) {
				ctrl.selectedBus = selectedRows;
			}
			
			ctrl.addBu = function(row) {
				return $q(function(resolved, rejected){
					var params = {};
					params.multiSelect = true;
					params.buCodes = [];
					angular.forEach(ctrl.group.bus, function(bu, index) {
						params.buCodes.push($filter('encode')(bu.buCode));
					});
					modalService.openCustomDialogs({
						'dialogsModule' : 'cda.bu',
						'params' : params,
						'btnCallback' : {
							'ok' : function(selectedRows) {
								if (!selectedRows || selectedRows.length == 0) {
									modalService.openErrorDialogs({
										messages : [ 'title.dialogs.info.norecsel' ]
									});
									return false;
								}
								if (!ctrl.group.bus) {
									ctrl.group.bus = [];
								}
								
								angular.forEach(selectedRows, function(selectedObj, index) {
									selectedRows[index].dataStatus = 'C';
									selectedRows[index].buTypeName = selectedRows[index].buType.split(":")[1];
									ctrl.group.bus.push(selectedRows[index]);
								});
								event.preventDefault();
								event.stopPropagation();
							},
							'close' : function(){
								rejected();
							}
						}
					});
				});
				
			};
			
		}
		return {
			initPrincipalRoles : initPrincipalRoles,
			initParentGroups : initParentGroups,
			initSubGroups : initSubGroups,
			initGroupUsers : initGroupUsers,
			initBus : initBus,
			getParentCompanyCodeOptions: getParentCompanyCodeOptions
		};
	}
})(angular);
